$(document).ready(function() {
    window.tableConstructor = new TableConstructor();
    window.stateLoader = new StateLoader();
    window.chart = new Chart();
    window.reportBuilder = new ReportBuilder();
    window.backendInterface = new BackendInterface();
    window.refreshAndSortTable = function(){};
    loadState({initial: true});
    prepareLayout();
    refreshLoop();
});
            
$(window).on('hashchange', function() {
    loadState();
    prepareLayout();
    fixForLiveData();
});
$(document).on("click", "#logout", function() {
    $.get(window.CONSTANTS.SERVICE_URL, {        
        request: 000,
        autAction: 'logout'
    }, function(response){
        if(response == "logged_out"){
            updateURL(00);
        }
    });
});
$(window).on('resize', function() {
    prepareLayout();
});
this.prepareLayout = function() {
    var wh = $(window).height();
    var hh = $(".header").height();
    var tmh = $(".topmenu").height();
    var th = $(".tabs").height();
    var smh = $(".status-message").height();
    $(".state-wrapper").css("height", 
        (wh - hh - tmh - th - smh - 20) + "px");
};
this.loadState = function(options){
    state = window.location.hash;
    if(state !== undefined && state !== null && state !== ''){
        state = state.split("#")[1];
        window.stateLoader.loadState(state);
        window.stateLoader.loadTabs(state, function(){
            updateMenu(state);
            updateTabs(state);
            verifyUser();
        });
    }
    else{
        if(options && options.initial){
            document.location.href = "#"+window.stateLoader.DEFAULT_STATE;
            verifyUser();
        }
    }
};
this.verifyUser = function(){
    
    $.get(window.CONSTANTS.SERVICE_URL, {
        request: 000,
        autAction: 'getUserData'
    },
    function(response) {
        if(response != "No User"){
            $("#currentUser").text("User: " + response);
        }
        else{
            $("#currentUser").text("User: No User");
            document.location.href = "#00";
        }
    });
};
this.updateMenu = function(state){
    var x = parseInt(state/10);
    $(".topmenu li").removeClass("active");
    $("#menu"+x).addClass("active");
};
this.updateTabs = function(state){
    $(".tab").removeClass("active");
    $("#tab"+state).addClass("active");
};
this.fixForLiveData = function(){
    if(window.aLiveDataFX != undefined){
        window.aLiveDataFX.loaded = false;
        window.aLiveDataFX.refresh = false;
    }
};
this.refreshLoop = function(){
    setInterval(function(){
        state = window.location.hash;
        if(state !== undefined && state !== null && state !== '')
            state = state.split("#")[1];

        if(window.aLiveDataFX != undefined && state == 40){
            window.aLiveDataFX.loadDataForActivitiesLDSilosTable();
            window.aLiveDataFX.loadDataForActivitiesLDMixersTable();
            window.aLiveDataFX.loadDataForActivitiesLDLinesTable();
        }
        else if(window.aLiveDataFX != undefined){
            window.aLiveDataFX.loaded = false;
            window.aLiveDataFX.refresh = false;
        }
    }, 5000);
}
$(document).on("click", ".tabs .tab", function(){
    var state = $(this).data("where").toString();
    updateURL(state);
});
$(document).on("click", ".topmenu li", function(){
    var state = $(this).data("where").toString();
    updateURL(state);
});
this.updateURL = function(state){
    document.location.href = "#"+state;
};

$(document).on('click', '.tbl .row.header .col.sortable', function(){
    window.sortTableItem = $(this);

    if(window.sortTableItem.find('.sort').hasClass('asc')){
        ascDesc = "desc";

    }
    else if(window.sortTableItem.find('.sort').hasClass('desc')){
        ascDesc = "";
    }
    else{
        ascDesc = "asc";
    }

    var tableClass = $(this).parent().parent().attr("id");
    var columnName = window.sortTableItem.text();

    window.refreshAndSortTable(tableClass, columnName, ascDesc);
});

//$(document).on('click', '.tbl .row.header .col.sort', function(){
//    if($(this).hasClass('_asc')){
//        var texta = $(this).html().split('▲')[0].trim();
//        $(this).removeClass('_asc').addClass('_desc').html(texta + '▼');
//    }else if($(this).hasClass('_desc')){
//        var textd = $(this).html().split('▼')[0].trim();
//        $(this).removeClass('_desc').html(textd + '▲▼');
//    }else{
//        var text = $(this).html().split('▲▼')[0].trim();
//        $(this).addClass('_asc').html(text + '▲');
//    }
//});


//prebaciti u packaging daily
//
//$(document).on("click", "#packagingDaily .preview", function() {
//        stateLoader.getPackagingDailyReport();
//    });
//
//    $(document).on("click", ".print", function() {
//        var url = $(this).data("doc-url") + "&print=true";
//        window.open(url, '_blank');
//    });
    

//nestedTable
/*  if ($(this).next(".nestedTable").length === 0) {
        $(this).after("<div class='nestedTable'></div>");
    }
    if ($(this).hasClass("opened")) {
        $(this).removeClass("opened");
    } else {
        $(this).addClass("opened")
            .next(".nestedTable")
            .html("<div class='tbl'>\n\
                    <div class='clearfix row data'>\n\
                        <div class='col w25'>10.07.2014.</div>\n\
                        <div class='col w25'>1</div>\n\
                        <div class='col w25'>8</div>\n\
                        <div class='col w25'>430</div>\n\
                    </div>\n\
                    <div class='clearfix row data'>\n\
                        <div class='col w25'>10.07.2014.</div>\n\
                        <div class='col w25'>1</div>\n\
                        <div class='col w25'>8</div>\n\
                        <div class='col w25'>430</div>\n\
                </div>\n\
                    <div class='clearfix row data'>\n\
                        <div class='col w25'>10.07.2014.</div>\n\
                        <div class='col w25'>1</div>\n\
                        <div class='col w25'>8</div>\n\
                        <div class='col w25'>430</div>\n\
                </div>\n\
                    <div class='clearfix row data'>\n\
                        <div class='col w25'>10.07.2014.</div>\n\
                        <div class='col w25'>1</div>\n\
                        <div class='col w25'>8</div>\n\
                        <div class='col w25'>430</div>\n\
                    </div>\n\
                </div>");
    }*/

    //$(this).next(".nestedTable").slideToggle();
    //var id = $(this).data("id");
